package com.future.demo.service;

import com.future.common.BaseService;
import com.future.common.CacheNames;
import com.future.demo.domain.Person;
import com.future.demo.repository.PersonRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * PERSON SERVICE
 *
 * @author WUWY
 * @create 2018-05-12 上午10:51
 **/
@Service
@CacheConfig(cacheNames = CacheNames.DEMOS)
public class PersonService extends BaseService<Person, String> {

    private static final Logger log = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    private PersonRepository repository;

    @Cacheable(key = "#p0")
    @Override
    public Person findStrictOne(String id) {
        log.debug("穿透缓存进来了");
        return super.findStrictOne(id);
    }

    @CacheEvict(key = "#p0")
    @Override
    public Person update(String id, Map<String, Object> map) {
        return super.update(id, map);
    }

    @CacheEvict(key = "#p0")
    @Override
    public void delete(String id) {
        super.delete(id);
    }

    public List<Person> findByFirstnameLikeOrderByLastnameDesc(String firstName) {
        return repository.findByFirstnameLikeOrderByLastnameDesc(firstName);
    }
}
